package chances.epg.data.taglib;

import java.util.Collection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import chances.epg.data.taglib.page.PageInfo;
import chances.epg.data.taglib.page.PageInfoHelper;


/**
 * 
 * @author Elysium
 *
 */
public abstract class QueryListTag extends QueryTag {

	/**
	 * serialVersionUID.
	 */
	private static final long serialVersionUID = 2812182155405954851L;
	protected String pageName;
	protected String varSize;
	protected int start = 0;
	protected boolean cycle = true;
	protected int pageNo = 1;

	
	public String getPageName() {
		return pageName;
	}
	public void setPageName(String pageName) {
		this.pageName = pageName;
	}
	
	protected PageInfo createPageInfo(int pageSize){
		PageInfo pageInfo = new PageInfo(pageSize);
		
		String curPageIndex = getCurPageIndex();
		if(StringUtils.isNotBlank(curPageIndex) && StringUtils.isNumeric(curPageIndex)){
			pageInfo = new PageInfo(Integer.parseInt(curPageIndex), pageSize);
		}
		
		return pageInfo;
	}
	
	protected String getCurPageIndex(){
		if(this.pageNo > 0) {
			return ""+this.pageNo;
		}
        //从请求参数中获取页数
	    String curPageIndexStr = pageContext.getRequest().getParameter(this.getPageIndexParamName());
        if (StringUtils.isNotBlank(curPageIndexStr) && StringUtils.isNumeric(curPageIndexStr)) {
            return curPageIndexStr;
        }
        
        //从pageContext中获取页数
        Object curPageIndexObject = pageContext.getAttribute(this.getPageIndexParamName());
        if (curPageIndexObject !=null && StringUtils.isNotBlank(curPageIndexObject.toString()) && StringUtils.isNumeric(curPageIndexObject.toString())) {
            return curPageIndexObject.toString();
        }
        
        return "";
	}
	
	/**
	 * 设置分页信息.
	 * @param pageInfo
	 */
	protected void setPageInfo(PageInfo pageInfo){
		//this.createPrevUri(pageInfo);
		//this.createNextUri(pageInfo);
		HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
		PageInfoHelper.getInstance().createTurnPageUrl(request, pageInfo, this.getPageIndexParamName(), this.isCycle());
		
		this.setAttribute(this.getPageName(), pageInfo);
	}
	
	/**
	 * 设置上一页链接.
	 * 如cycle为false，则前一页链接可能为空.
	 * @param pageInfo
	 */
	/*
	private void createPrevUri(PageInfo pageInfo){
		PageBean pageBean = pageInfo.getPageBean();
		
		int curPageIndex = 1;
        if(pageBean.getCurrentPage() > 1){
			curPageIndex = pageBean.getCurrentPage() - 1;
			pageInfo.setPrevUri(this.generatorPageURL(curPageIndex));
		}else{
			if(this.isCycle()){
				curPageIndex = pageBean.getPageCount();
				pageInfo.setPrevUri(this.generatorPageURL(curPageIndex));
			}
		}
	}
	*/
	
	/**
	 * 设置下一页链接.
	 * 如cycle为false，则前一页链接可能为空.
	 * @param pageInfo
	 */
	/*
	private void createNextUri(PageInfo pageInfo) {
        PageBean pageBean = pageInfo.getPageBean();
		
		int curPageIndex = 1;
		
        if(pageBean.getCurrentPage() < pageBean.getPageCount()){
			curPageIndex = pageBean.getCurrentPage() + 1;
			pageInfo.setNextUri(this.generatorPageURL(curPageIndex));
		}else{
			if(this.isCycle()){
				curPageIndex = 1;
				pageInfo.setNextUri(this.generatorPageURL(curPageIndex));
			}
		}
	}
	*.
	
	/**
	 * 根据页索引生成翻页信息.
	 * @param curPageIndex
	 * @return
	 */
	/*
	private String generatorPageURL(int curPageIndex) {
		AccessContext context = AccessContextUtils.getAccessContext(pageContext);
		String uri = context.getRequestURI();
		String queryString = context.getQueryString();
		
		//当前访问链接上无参数
		if(StringUtils.isBlank(queryString)){
		    queryString = this.getPageIndexParamName() + "=" + curPageIndex;
		    return uri + "?" + queryString;
		}
		
		//替换页索引参数
		Pattern p= Pattern.compile(this.getPageIndexParamName() + "=\\d*");
		Matcher matcher = p.matcher(queryString);
		if(matcher.find()){
			queryString = matcher.replaceFirst(this.getPageIndexParamName() + "=" + curPageIndex);
		}else{
			queryString += "&";
			queryString += this.getPageIndexParamName();
			queryString += "=";
			queryString += curPageIndex;
		}
		
		return uri + "?" + queryString;
	}
	*/
	
	/**
	 * 请求中页索引参数名称.
	 * @return
	 */
	private String getPageIndexParamName() {
		return this.pageName + "_idx";
	}
	
	/**
	 * 设置列表结果
	 * @param result
	 */
	protected void setCollectionResult(Collection<?> result){
		if(StringUtils.isNotBlank(this.varSize)){
			this.setAttribute(this.varSize, result.size());
		}
		
		this.setResult(result);
	}
	
	public boolean isCycle() {
		return cycle;
	}
	public void setCycle(boolean cycle) {
		this.cycle = cycle;
	}
	
	public String getVarSize() {
		return varSize;
	}
	public void setVarSize(String varSize) {
		this.varSize = varSize;
	}
	
	
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getPageNo() {
		return pageNo;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public static void main(String[] args) {
		Pattern p= Pattern.compile("abc=\\d*");
		
		String queryString = "abc=123&param1=1111";
		
		Matcher matcher = p.matcher(queryString);
		if(matcher.find()){
			System.out.println("found");
		}else{
			System.out.println("not found");
		}
		System.out.println(matcher.replaceFirst("abc=1"));
        
        queryString = "abc=123";
        matcher = p.matcher(queryString);
        if(matcher.find()){
			System.out.println("found");
		}else{
			System.out.println("not found");
		}
        System.out.println(matcher.replaceFirst("abc=1"));
        
        queryString = "param1=1111&abc=123";
        matcher = p.matcher(queryString);
        if(matcher.find()){
			System.out.println("found");
		}else{
			System.out.println("not found");
		}
        
        queryString = "param1=1111&abc=123abc";
        matcher = p.matcher(queryString);
        if(matcher.find()){
			System.out.println("found");
		}else{
			System.out.println("not found");
		}
        System.out.println(matcher.replaceFirst("abc=1"));
        
        
        
	}

}
